package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.CustomerMainOriginal.rCustomerMain;

public class CustomerInfoFragment extends BaseFragment implements OnClickListener {

  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.EXTRA_VALUE";
  private static final int REQUEST_DETAIL = 0;//客户详情
  //private BaseVolleyActivity mActivity;
  private rCustomerMain mCustomerDetail;
  private String mCustomerName;
  private String mCustomerRegion;

  public static Fragment newInstance(rCustomerMain customerDetail) {
    Bundle argsBundle = new Bundle();
    argsBundle.putSerializable(EXTRA_VALUE, customerDetail);

    CustomerInfoFragment fragment = new CustomerInfoFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.customer_detail);

    //if (NavUtils.getParentActivityName(getActivity()) != null) {
    //
    //	( (BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //}
    //
    //( (BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.customer_detail);
    mCustomerDetail = (rCustomerMain) getArguments().getSerializable(EXTRA_VALUE);
    if (mCustomerDetail != null) {
      mCustomerName = mCustomerDetail.getCustomerName();
      mCustomerRegion = mCustomerDetail.getDistrictDesc();
    }
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_customer_info, container, false);
    TextView mUserNameHeader = (TextView) view.findViewById(R.id.tv_userName_header);
    TextView mCustomerLocation = (TextView) view.findViewById(R.id.tv_describe_header);
    TextView mCheckDetailCustomerInfo =
        (TextView) view.findViewById(R.id.tv_checkDetail_customerInfo);
    TextView mDepartmentRelationShipCustomerInfo =
        (TextView) view.findViewById(R.id.tv_departmentRelationShip_customerInfo);
    TextView mNeedCustomerInfo = (TextView) view.findViewById(R.id.tv_need_customerInfo);
    TextView mCustomerBill = (TextView) view.findViewById(R.id.tv_customer_bill);
    TextView mVisitCustomerInfo = (TextView) view.findViewById(R.id.tv_visit_customerInfo);
    mUserNameHeader.setText(mCustomerName);
    mCustomerLocation.setText(mCustomerRegion);
    mCheckDetailCustomerInfo.setOnClickListener(this);
    mDepartmentRelationShipCustomerInfo.setOnClickListener(this);
    mNeedCustomerInfo.setOnClickListener(this);
    mCustomerBill.setOnClickListener(this);
    mVisitCustomerInfo.setOnClickListener(this);
    return view;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.tv_checkDetail_customerInfo:
        Intent customerDetailTntent = new Intent(getActivity(), CustomerDetailActivity.class);
        customerDetailTntent.putExtra(CustomerDetailFragment.EXTRA_CUSTOMER_DETAIL,
            mCustomerDetail);
        customerDetailTntent.putExtra(CustomerDetailFragment.EXTRA_TYPE,
            CustomerDetailFragment.VALUE_READ);
        startActivityForResult(customerDetailTntent, REQUEST_DETAIL);
        break;

      case R.id.tv_departmentRelationShip_customerInfo:
        Intent cRSIntent = new Intent(getActivity(), CustomerRelationDepartmentPagerActivity.class);
        startActivity(cRSIntent);
        break;

      case R.id.tv_need_customerInfo:
        Intent intent = new Intent(getActivity(), CustomerNeedListActivity.class);
        intent.putExtra(CustomerNeedListFragment.EXTRA_VALUE, mCustomerDetail);
        startActivity(intent);
        break;
      case R.id.tv_customer_bill:
        BaseVolleyActivity.newInstance(this, CustomerBillListActivity.class,
            new BaseParams<rCustomerMain>().setItem(mCustomerDetail), 0);
        break;
      case R.id.tv_visit_customerInfo:
        Intent visitintent = new Intent(getActivity(), SignFieldActivity.class);
        visitintent.putExtra(CustomerVisitFragment.EXTRA_VALUE, mCustomerDetail);
        startActivity(visitintent);
        break;

      default:
        break;
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    switch (requestCode) {
      case REQUEST_DETAIL:
        getActivity().setResult(Activity.RESULT_OK);
        getActivity().finish();

        break;

      default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
